﻿using RotovapPlatform.Domain.Business.Storage.Entities.Base;
using RotovapPlatform.Domain.Business.Storage.Entities.Bottles;
using RotovapPlatform.Domain.Business.Storage.Enums;
using static RotovapPlatform.Domain.Business.Machine.Enums.ExperimentConfigEnums;

namespace RotovapPlatform.Domain.Business.Storage.Entities;

public class StorageLocation
{
	public int Id { get; private set; }                         // 主键
	public LocationCode LocationCode { get; private set; }      // 位置代号
	public int Position { get; private set; }                   // 具体位置编号
	public bool IsOccupied { get; private set; }               // 是否占用
	public int IsReserved { get; set; }               // 0:未预占位,1:预占位，未使用，2:预占位，已使用
	public List<ItemType> SupportedItemTypes { get; private set; } // 支持的物品类型
	public IStorageItem? CurrentItem { get; private set; }  // 当前存放的物品

	// 基本构造函数
	public StorageLocation(LocationCode locationCode, int position, List<ItemType> supportedItemTypes)
	{
		LocationCode = locationCode;
		Position = position;
		SupportedItemTypes = supportedItemTypes;
		IsOccupied = false;
		IsReserved = 0;
	}

	// 带瓶子的构造函数
	public StorageLocation(LocationCode locationCode, int position, List<ItemType> supportedItemTypes, IStorageItem? item)
		: this(locationCode, position, supportedItemTypes)
	{
		if (item != null)
		{
			SetItem(item);
		}
	}

	public void SetItem(IStorageItem item)
	{
		if (!SupportedItemTypes.Contains(item.Type))
		{
			throw new InvalidOperationException($"Storage location does not support {item.Type} type");
		}

		CurrentItem = item;
		IsOccupied = true;
	}

	public IStorageItem? RemoveItem()
	{
		var item = CurrentItem;
		CurrentItem = null;
		IsOccupied = false;
		return item;
	}


	/// <summary>
	/// 更新预占位状态
	/// </summary>
	/// <param name="value">预占位状态值（0:未预占位,1:预占位，未使用，2:预占位，已使用）</param>
	/// <exception cref="ArgumentOutOfRangeException">当状态值不在合法范围内时抛出</exception>
	public void UpdateIsReserved(int value)
	{
		if (value < 0 || value > 2)
		{
			throw new ArgumentOutOfRangeException(nameof(value), "预占位状态值必须在0-2范围内");
		}

		IsReserved = value;
	}
}